﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Text.RegularExpressions;
using CalculatorLogic;
using CalculatorLogic.Algebraic_Math;

namespace CalculatorGUI
{
    public partial class MainPage2 : PhoneApplicationPage
    {
        private bool graphing = false;
        // Constructor
        public MainPage2()
        {
            InitializeComponent();
            textBox1.Text = "";
            
        }
        EquationParser parser = new CalculatorLogic.EquationParser();
        PostfixSolver solver = new CalculatorLogic.PostfixSolver();
        CalculatorLogic.Algebraic_Math.Solve algebraSolver = new CalculatorLogic.Algebraic_Math.Solve();
        Factor factorSolver = new CalculatorLogic.Algebraic_Math.Factor();
        Expand expandSolver = new CalculatorLogic.Algebraic_Math.Expand();
        Zeros zerosSolver = new CalculatorLogic.Algebraic_Math.Zeros();

        Equation equation = new Equation();

        #region Numbers
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "1";
        }    

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "2";
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "3";
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "4";
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "5";
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "6";
        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "7";
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "8";
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "9";
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "0";
        }
        #endregion Numbers

        #region Symbols
        private void button14_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += ".";
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " -";
        }

        private void button28_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ( ";
        }

        private void button29_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ) ";
        }

        private void button22_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " ^ ";
        }

        private void button23_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " = ";
        }

        private void button30_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += "x";
        }

        private void button36_Click_1(object sender, RoutedEventArgs e)
        {
            textBox1.Text += Math.PI;
        }

        private void button37_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += Math.E;
        }
        #endregion Symbols

        #region Basic Math
        private void button8_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " + ";
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " - ";
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " * ";
        }

        private void button20_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " / ";
        }
        #endregion Basic Math

        #region Trig
        private void button17_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " SIN ( ";
        }

        private void button18_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " COS ( ";
        }

        private void button19_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " TAN ( ";
        }

        private void button33_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " CSC ( ";
        }

        private void button34_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " COT ( ";
        }

        private void button35_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " SEC ( ";
        }
        #endregion Trig

        #region Logarithmic Functions
        private void button21_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " LOG ( ";
        }

        private void button27_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text += " LN ( ";
        }
        #endregion Logarithmic Functions

        #region Calculator Functions
        private void button32_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = string.Empty;
        }

        private void button31_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length-1);
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (GetLastLine(textBox1.Text).Contains("="))
            {
                equation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
                algebraSolver.Apply(equation);
                textBox1.Text += " = \r";
                textBox1.Text += equation.Solved;
                textBox1.Text += "\r";
            }

            else
            {
                string answer = Solve(GetLastLine(textBox1.Text));
                textBox1.Text += " = \r";
                textBox1.Text += answer.PadLeft(65 - (answer.Length));
                textBox1.Text += "\r";
            }
            autoScroll();
        }
        #endregion Calculator Functions

        #region Algebraic Functions
        private void button24_Click(object sender, RoutedEventArgs e)
        {
            equation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
            factorSolver.Apply(equation);
            textBox1.Text += " = \r";
            textBox1.Text += equation.Solved;
            textBox1.Text += "\r";

            equation.LastAnswers.Add(equation.Solved);
            autoScroll();
        }

        private void button25_Click(object sender, RoutedEventArgs e)
        {
            string expand = GetLastLine(textBox1.Text);
            expand = expand.Replace(" ", "");
            expand = expand.Replace("+", " + ");
            equation.Input[0] = expand.Trim();
            expandSolver.Apply(equation);
            textBox1.Text += " = \r";
            textBox1.Text += equation.Solved;
            textBox1.Text += "\r";

            equation.LastAnswers.Add(equation.Solved);
            autoScroll();
        }

        private void button26_Click(object sender, RoutedEventArgs e)
        {
            equation.Input[0] = GetLastLine(textBox1.Text).Replace(" ", "");
            zerosSolver.Apply(equation);
            textBox1.Text += " = \r";
            textBox1.Text += equation.Solved;
            textBox1.Text += "\r";

            equation.LastAnswers.Add(equation.Solved);
            autoScroll();
        }
        #endregion Algebraic Functions

        #region Methods
        public string Solve(string input)
        {
            string temp = parser.Parse(input);
            string answer = solver.Solve(temp,equation);
            return answer;
        }

        public string GetLastLine(string text)
        {
            string[] lines = Regex.Split(textBox1.Text, "\r");
            return lines.Last();
        }

        /// <summary>
        /// Scrolls the textbox automatically as the height increases
        /// </summary>
        private void autoScroll()
        {
            if (textBox1.ActualHeight > scrollViewer1.ViewportHeight)
            {
                scrollViewer1.ScrollToVerticalOffset(scrollViewer1.ScrollableHeight + textBox1.ActualHeight);
            }
        }

        private void graphingGUI()
        {
            if (graphing)
            {
                scrollViewer1.Visibility = Visibility.Collapsed;
                graphControl1.Visibility = Visibility.Visible;
            }
            else
            {
                scrollViewer1.Visibility = Visibility.Visible;
                graphControl1.Visibility = Visibility.Collapsed;
            }
        }
        #endregion Methods
    }
}